home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr49 / 105_01.zip / DIO.C < prev    next >
Text File  |  1993-06-09  |  7KB  |  263 lines

  1. /*
  2.     Directed I/O package for use with BDS C v1.4x.   LZ -- 4/81
  3.  
  4.     The following functions make up the directed I/O library:
  5.  
  6.     1. dioinit(&argc,argv)        Make this the first thing you do in
  7.                     your "main" function, to process
  8.                     redirection commands on the CP/M
  9.                     command line.
  10.  
  11.     2. getchar()            Gets a character from the keyboard,
  12.                     or from a directed input file if one
  13.                     was specified on the command line.
  14.  
  15.     3. putchar(c)            Puts a character out to the console,
  16.                     or to a directed output file if one
  17.                     was specified on the command line.
  18.  
  19.     4. dioflush()            Flushes directed output file, if open,
  20.                     and closes all directed I/O files (if
  21.                     any.) This must be called before your
  22.                     program exits or returns to CP/M.
  23.  
  24.     To activate redirection: Four special arguments may be given
  25.     on the command line to the generated COM file...
  26.  
  27.         >foo    causes "putchar" to place characters into the file
  28.             named "foo" instead of to the console.
  29.  
  30.         +foo    like >foo except that the characters are ALSO sent
  31.             to the console.
  32.  
  33.         <foo    causes "getchar" to return characters from the file
  34.             named "foo" instead of from the keyboard.
  35.  
  36.      command |prog    causes the standard output of the command specified in
  37.             "command" to be fed into the standard input of another
  38.             program, "prog". (BOTH "command" and "prog" must be
  39.             compiled with DIO)
  40.  
  41.      (Note that there must never be any spaces between >,+,< or | and the
  42.       corresponding filename.)
  43.  
  44.     Thus, a C program using redirection has the following form:
  45.  
  46.         #include "bdscio.h"        /* standard header file    */
  47.         #include "dio.h"        /* directed I/O header    */
  48.  
  49.         ...                /* other externals, if any */
  50.  
  51.         main(argc,argv)
  52.         char **argv;
  53.         {
  54.             ...            /* declarations        */
  55.             dioinit(&argc,argv)    /* initialize redirection */
  56.             ...            /* body of program    */
  57.             dioflush();
  58.         }
  59.             
  60.     NOTES:
  61.  
  62.     0. Redirection and pipes work only for TEXT. This mechanism should
  63.        not be used for binary data.
  64.  
  65.     1. The "getchar" and "putchar" functions should each be used EXPLICITLY
  66.        at least once in your main source file, so that the correct versions
  67.        are picked off from DIO.CRL instead of the incorrect ones from
  68.        DEFF2.CRL (because of the way the linker works.)
  69.  
  70.     2. The "putc" library function should be modified so that an iobuf
  71.        value of 4 sends a character to the CP/M console via a "bdos"
  72.        call (as opposed to using "putchar"), and that a '\n' character
  73.        thus sent should be expanded into a CR-LF combination. This
  74.            is easily accomplished by adding the following clause to the "putc"
  75.        function, recompiling STDLIB1.C, and updating DEFF.CRL by
  76.        transferring in the new "putc" with CLIB.COM:
  77.  
  78.         if (_iobuf == 4) {
  79.             if (c == '\n') bdos(2,'\r');
  80.             bdos(2,c);
  81.         }            
  82.        (This may already have been done in the version you have.)
  83.  
  84.     3. The "execv" function, used by this package, is available in the
  85.        file EXECV.ASM; it should be assembled, renamed EXECV.CRL, and
  86.        then transferred into DEFF2.CRL using CLIB.COM.
  87.        (This may already have been done in the version you have.)
  88.  
  89. */
  90.  
  91. #include "bdscio.h"
  92. #include "dio.h"
  93.  
  94. #define CON_INPUT 1            /* BDOS call to read console       */
  95. #define CON_OUTPUT 2            /* BDOS call to write to console   */
  96. #define CON_STATUS 11            /* BDOS call to interrogate status */
  97.  
  98. #define CONTROL_C 3            /* Quit character           */
  99. #define STDERR 4            /* Standard Error descriptor (sorry,
  100.                        Unix fans, 2 was already used.) */
  101. #define INPIPE 2            /* bit setting to indicate directed
  102.                        input from a temp. pipe fil     */
  103. #define VERBOSE 2            /* bit setting to indicate output is to
  104.                        go to console AND directed output */
  105.  
  106. /* 
  107.     The "dioinit" function must be called at the beginning of the
  108.     "main" function:
  109. */
  110.  
  111. #define argc *argcp
  112.  
  113. dioinit(argcp,argv)
  114. int *argcp;
  115. char **argv;
  116. {
  117.     int i,j, argcount;
  118.  
  119.     _diflag = _doflag = _pipef = FALSE;  /* No directed I/O by default   */
  120.     _nullpos = &argv[argc];
  121.     argcount = 1;
  122.  
  123.     for (i = 1; i < argc; i++)    /* Scan the command line for > and < */
  124.     {
  125.         if (_pipef) break;
  126.         switch(*argv[i]) {
  127.  
  128.            case '<':        /* Check for directed input: */
  129.             if (!argv[i][1]) goto barf;
  130.             if (fopen(&argv[i][1], _dibuf) == ERROR)
  131.             {
  132.                 fprintf(STDERR,"Can't open %s\n",&argv[i][1]);
  133.                 exit();
  134.             }
  135.             _diflag = TRUE;
  136.             if (strcmp(argv[i],"<TEMPIN.$$$") == 0)
  137.                  _diflag |= INPIPE;
  138.             goto movargv;
  139.  
  140.            case '|':    /* Check for pipe: */
  141.             _pipef++;
  142.             _pipedest = &argv[i][1]; /* save prog name for execl */
  143.             if (argv[i][1]) 
  144.             {
  145.                 argv[i] = ".TEMPOUT.$$$";  /* temp. output */
  146.                 _savei = &argv[i];
  147.             }
  148.             goto foo;
  149.  
  150.            case '+': 
  151.             _doflag |= VERBOSE;
  152.             
  153.          foo:   case '>':    /* Check for directed output    */
  154.         
  155.             if (!argv[i][1]) 
  156.             {
  157.             barf:   fprintf(STDERR,"Bad redirection/pipe specifier");
  158.                 exit();
  159.             }
  160.             unlink(&argv[i][1]);
  161.             if (fcreat(&argv[i][1], _dobuf) == ERROR)
  162.             {
  163.                    fprintf(STDERR,"Can't create %s\n",&argv[i][1]);
  164.                    exit();
  165.             }
  166.             _doflag++;
  167.  
  168.          movargv:    if (!_pipef) {
  169.                 for (j = i; j < argc; j++) argv[j] = argv[j+1];
  170.                 (argc)--;
  171.                 i--;
  172.                 _nullpos--;
  173.              } else {
  174.                 argc = argcount;
  175.                 argv[argc] = 0;
  176.              }
  177.             break;
  178.  
  179.             default:    /* handle normal arguments: */
  180.             argcount++;
  181.         }
  182.     }
  183. }
  184.  
  185.  
  186. #undef argc
  187.  
  188. /*
  189.     The "dioflush" function must be called before exiting the program:
  190. */
  191.  
  192. dioflush()
  193. {
  194.     if (_diflag)
  195.     {
  196.         fclose(_dibuf);
  197.         if (_diflag & INPIPE) unlink("tempin.$$$");
  198.     }
  199.  
  200.     if (_doflag)
  201.     {
  202.         putc(CPMEOF,_dobuf);
  203.         fflush(_dobuf);
  204.         fclose(_dobuf);
  205.         rename("tempout.$$$","tempin.$$$");
  206.         if (_pipef) 
  207.         {
  208.             *_savei = "<TEMPIN.$$$";
  209.             *_nullpos = NULL;
  210.             execv(_pipedest,_savei);
  211.         }
  212.     }
  213. }
  214.  
  215.  
  216. /*
  217.     This version of "getchar" replaces the regular version when using
  218.     directed I/O:
  219. */
  220.  
  221. getchar()
  222. {
  223.     char c;
  224.  
  225.     if (_diflag) {
  226.         if ((c = getc(_dibuf)) == '\r') c = getc(_dibuf);
  227.     } else
  228.         if ((c = bdos(CON_INPUT)) == CONTROL_C) exit();
  229.  
  230.     if (c == CPMEOF) return EOF;         /* Control-Z is EOF key     */
  231.     if (c == '\r') 
  232.     {
  233.         c = '\n';
  234.         if (!_diflag) bdos(2,'\n');  /* echo LF after CR to console */
  235.     }
  236.     return c;
  237. }
  238.  
  239.  
  240. /*
  241.     This version of "putchar" replaces the regular version when using
  242.     directed I/O:
  243. */
  244.  
  245. putchar(c)
  246. char c;
  247. {
  248.     if (_doflag)
  249.     {
  250.         if (c == '\n') putc('\r',_dobuf);
  251.         if(putc(c,_dobuf) == ERROR)
  252.         {
  253.             fprintf(STDERR,"File output error; disk full?\n");
  254.             exit();
  255.         }
  256.         if (!(_doflag & VERBOSE)) return;
  257.     }
  258.  
  259.     if (bdos(CON_STATUS) && bdos(CON_INPUT) == CONTROL_C) exit();
  260.     if (c == '\n') bdos(CON_OUTPUT,'\r');
  261.     bdos(CON_OUTPUT,c);
  262. }
  263.